package com.hl.qzzpbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hl.qzzpbackend.common.DeleteRequest;
import com.hl.qzzpbackend.exception.BusinessException;
import com.hl.qzzpbackend.exception.ErrorCode;
import com.hl.qzzpbackend.exception.ThrowUtils;
import com.hl.qzzpbackend.model.dto.industryType.IndustryTypeAddRequest;
import com.hl.qzzpbackend.model.dto.industryType.IndustryTypeQueryRequest;
import com.hl.qzzpbackend.model.dto.industryType.IndustryTypeUpdateRequest;
import com.hl.qzzpbackend.model.entity.IndustryType;
import com.hl.qzzpbackend.model.enums.StatusEnum;
import com.hl.qzzpbackend.model.vo.IndustryTypeAndPositionTypeVO;
import com.hl.qzzpbackend.model.vo.IndustryTypeVO;
import com.hl.qzzpbackend.service.IndustryTypeService;
import com.hl.qzzpbackend.mapper.IndustryTypeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 行业类型 业务层
 *
 * @author helong
 */
@Service
public class IndustryTypeServiceImpl extends ServiceImpl<IndustryTypeMapper, IndustryType>
    implements IndustryTypeService{

    @Resource
    private IndustryTypeMapper industryTypeMapper;

    /**
     * 添加行业类型
     * @param industryTypeAddRequest 行业类型添加请求
     * @return 是否成功
     */
    @Override
    public boolean industryTypeAdd(IndustryTypeAddRequest industryTypeAddRequest) {
        String name = industryTypeAddRequest.getName();

        // 参数校验
        long count = this.lambdaQuery().eq(IndustryType::getName, name).count();
        ThrowUtils.throwIf(count > 0, ErrorCode.PARAMS_ERROR, "行业类型已存在");

        IndustryType industryType = new IndustryType();
        industryType.setName(name);


        boolean result =  this.save(industryType);
        if(!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "添加行业类型失败");
        }
        return true;
    }

    /**
     * 修改行业类型
     * @param industryTypeUpdateRequest 修改行业类型请求类
     * @return 是否成功
     */
    @Override
    public boolean industryTypeUpdate(IndustryTypeUpdateRequest industryTypeUpdateRequest) {
        Long id = industryTypeUpdateRequest.getId();
        String name = industryTypeUpdateRequest.getName();

        // 参数校验
        long count1 = this.lambdaQuery().eq(IndustryType::getId,id).count();
        ThrowUtils.throwIf(count1 == 0, ErrorCode.PARAMS_ERROR, "行业类型不存在");

        long count2 = this.lambdaQuery().eq(IndustryType::getName, name).count();
        ThrowUtils.throwIf(count2 > 0, ErrorCode.PARAMS_ERROR, "行业类型已存在");

        IndustryType industryType = new IndustryType();
        BeanUtil.copyProperties(industryTypeUpdateRequest, industryType);

        boolean result = this.updateById(industryType);
        if(!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "修改行业类型失败");
        }
        return true;
    }

    /**
     * 删除行业类型
     * @param deleteRequest 删除请求类
     * @return 是否成功
     */
    @Override
    public boolean industryTypeDelete(DeleteRequest deleteRequest) {
        Long id = deleteRequest.getId();

        // 参数校验
        IndustryType oldIndustryType = this.getById(id);
        ThrowUtils.throwIf(oldIndustryType == null, ErrorCode.PARAMS_ERROR, "行业类型不存在");

        // todo 删除行业类型前，需要判断该行业类型是否被使用

        // todo 删除行业类型前，需要判断该行业类型是否被岗位类型引用

        boolean result = this.removeById(id);
        if(!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "删除行业类型失败");
        }
        return true;
    }

    /**
     * 行业类型分页查询
     * @param industryTypeQueryRequest 行业类型分页查询请求类
     * @return pageVO
     */
    @Override
    public Page<IndustryTypeVO> getIndustryTypePage(IndustryTypeQueryRequest industryTypeQueryRequest) {
        int current = industryTypeQueryRequest.getCurrent();
        int pageSize = industryTypeQueryRequest.getPageSize();

        LambdaQueryWrapper<IndustryType> queryWrapper = getQueryWrapper(industryTypeQueryRequest);
        Page<IndustryType> page = this.page(new Page<>(current,pageSize), queryWrapper);
        List<IndustryType> industryTypeList = page.getRecords();
        Page<IndustryTypeVO> pageVO = new Page<>(current,pageSize,page.getTotal());
        pageVO.setRecords(getIndustryTypeListVO(industryTypeList));

        return pageVO;
    }

    /**
     * 获取查询条件
     * @param industryTypeQueryRequest 行业类型分页查询请求类
     * @return 查询条件
     */
    @Override
    public LambdaQueryWrapper<IndustryType> getQueryWrapper(IndustryTypeQueryRequest industryTypeQueryRequest){
        String name = industryTypeQueryRequest.getName();
        return Wrappers.lambdaQuery(IndustryType.class)
                .like(StringUtils.isNotBlank(name), IndustryType::getName, name)
                .orderByDesc(IndustryType::getCreateTime);
    }

    /**
     * 获取行业类型VO
     * @param industryType 行业类型
     * @return 行业类型VO
     */
    @Override
    public IndustryTypeVO getIndustryTypeVO(IndustryType industryType){
        ThrowUtils.throwIf(industryType == null, ErrorCode.PARAMS_ERROR, "行业类型不存在");
        IndustryTypeVO industryTypeVO = new IndustryTypeVO();
        BeanUtil.copyProperties(industryType, industryTypeVO);
        return industryTypeVO;
    }

    /**
     * 获取行业类型VO列表
     * @param industryTypeList 行业类型列表
     * @return 行业类型VO列表
     */
    @Override
    public List<IndustryTypeVO> getIndustryTypeListVO(List<IndustryType> industryTypeList){
        if(CollUtil.isEmpty(industryTypeList)){
            return Collections.emptyList();
        }
        return industryTypeList.stream().map(this::getIndustryTypeVO).collect(Collectors.toList());
    }




    /**
     * 获取所有行业类型且包含岗位类型
     * @return 所有行业类型且包含岗位类型
     */
    @Override
    public List<IndustryTypeAndPositionTypeVO> getListWithPositionType() {
        return industryTypeMapper.getListWithPositionType();
    }

    /**
     * 获取所有行业类型
     * @return 所有行业类型
     */
    @Override
    public List<IndustryTypeVO> getIndustryTypeVOList() {
        return industryTypeMapper.getIndustryTypeVOList();
    }

}




